/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include "exl.h"

#define r0			thread_self->r0
#define ss			thread_self->ess
#define bp			thread_self->ebp
#define sp			thread_self->esp
#define obj			thread_self->obj
#define ret			thread_self->ret
#define flt			thread_self->flt

/*
 * Implementation
 */
void
evm_d_ne(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 != r0.v.d;
	    break;
	case t_int:
	    r0.v.i = v->v.i != r0.v.d;
	    break;
	case t_float:
	    r0.v.i = v->v.d != r0.v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_get_d(v->v.mpz) != r0.v.d;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_get_d(v->v.mpq) != r0.v.d;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_d(thr_rr0, r0.v.d, thr_rnd);
	    r0.v.i = !mpfr_equal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    r0.v.i = 1;
	    break;
    }
}

void
evm_d_lt(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 < r0.v.d;
	    break;
	case t_int:
	    r0.v.i = v->v.i < r0.v.d;
	    break;
	case t_float:
	    r0.v.i = v->v.d < r0.v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_get_d(v->v.mpz) < r0.v.d;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_get_d(v->v.mpq) < r0.v.d;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_d(thr_rr0, r0.v.d, thr_rnd);
	    r0.v.i = mpfr_less_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_d_le(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 <= r0.v.d;
	    break;
	case t_int:
	    r0.v.i = v->v.i <= r0.v.d;
	    break;
	case t_float:
	    r0.v.i = v->v.d <= r0.v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_get_d(v->v.mpz) <= r0.v.d;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_get_d(v->v.mpq) <= r0.v.d;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_d(thr_rr0, r0.v.d, thr_rnd);
	    r0.v.i = mpfr_lessequal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_d_eq(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 == r0.v.d;
	    break;
	case t_int:
	    r0.v.i = v->v.i == r0.v.d;
	    break;
	case t_float:
	    r0.v.i = v->v.d == r0.v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_get_d(v->v.mpz) == r0.v.d;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_get_d(v->v.mpq) == r0.v.d;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_d(thr_rr0, r0.v.d, thr_rnd);
	    r0.v.i = mpfr_equal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    r0.v.i = 0;
	    break;
    }
}

void
evm_d_ge(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 >= r0.v.d;
	    break;
	case t_int:
	    r0.v.i = v->v.i >= r0.v.d;
	    break;
	case t_float:
	    r0.v.i = v->v.d >= r0.v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_get_d(v->v.mpz) >= r0.v.d;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_get_d(v->v.mpq) >= r0.v.d;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_d(thr_rr0, r0.v.d, thr_rnd);
	    r0.v.i = mpfr_greaterequal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_d_gt(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 > r0.v.d;
	    break;
	case t_int:
	    r0.v.i = v->v.i > r0.v.d;
	    break;
	case t_float:
	    r0.v.i = v->v.d > r0.v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_get_d(v->v.mpz) > r0.v.d;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_get_d(v->v.mpq) > r0.v.d;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_d(thr_rr0, r0.v.d, thr_rnd);
	    r0.v.i = mpfr_greater_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_d_add(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.d = 0.0 + r0.v.d;
	    break;
	case t_int:
	    r0.v.d = v->v.i + r0.v.d;
	    break;
	case t_float:
	    r0.v.d = v->v.d + r0.v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.d = mpz_get_d(v->v.mpz) + r0.v.d;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.d = mpq_get_d(v->v.mpq) + r0.v.d;
	    break;
	case t_mpr:		case t_shr:
	    er_add_r_d(thr_rr, v->v.mpr, r0.v.d);
	    set_rr();
	    break;
	case t_cdd:
	    r0.v.cdd = v->v.cdd + r0.v.d;
	    chk_rd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_add_qq_d(v->v.cqq, r0.v.d);
	    chk_rd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_add_cc_d(thr_c, v->v.mpc, r0.v.d);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_add_float(v->v.o, r0.v.d, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_d_sub(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.d = 0.0 - r0.v.d;
	    break;
	case t_int:
	    r0.v.d = v->v.i - r0.v.d;
	    break;
	case t_float:
	    r0.v.d = v->v.d - r0.v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.d = mpz_get_d(v->v.mpz) - r0.v.d;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.d = mpq_get_d(v->v.mpq) - r0.v.d;
	    break;
	case t_mpr:		case t_shr:
	    er_sub_r_d(thr_rr, v->v.mpr, r0.v.d);
	    set_rr();
	    break;
	case t_cdd:
	    r0.v.cdd = v->v.cdd - r0.v.d;
	    chk_rd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_sub_qq_d(v->v.cqq, r0.v.d);
	    chk_rd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_sub_cc_d(thr_c, v->v.mpc, r0.v.d);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_sub_float(v->v.o, r0.v.d, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_d_mul(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.d = 0.0 * r0.v.d;
	    break;
	case t_int:
	    r0.v.d = v->v.i * r0.v.d;
	    break;
	case t_float:
	    r0.v.d = v->v.d * r0.v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.d = mpz_get_d(v->v.mpz) * r0.v.d;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.d = mpq_get_d(v->v.mpq) * r0.v.d;
	    break;
	case t_mpr:		case t_shr:
	    er_mul_r_d(thr_rr, v->v.mpr, r0.v.d);
	    set_rr();
	    break;
	case t_cdd:
	    r0.v.cdd = v->v.cdd * r0.v.d;
	    chk_rd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_mul_qq_d(v->v.cqq, r0.v.d);
	    chk_rd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_mul_cc_d(thr_c, v->v.mpc, r0.v.d);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_mul_float(v->v.o, r0.v.d, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_d_div(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.d = 0.0 / r0.v.d;
	    break;
	case t_int:
	    r0.v.d = v->v.i / r0.v.d;
	    break;
	case t_float:
	    r0.v.d = v->v.d / r0.v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.d = mpz_get_d(v->v.mpz) / r0.v.d;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.d = mpq_get_d(v->v.mpq) / r0.v.d;
	    break;
	case t_mpr:		case t_shr:
	    er_div_r_d(thr_rr, v->v.mpr, r0.v.d);
	    set_rr();
	    break;
	case t_cdd:
	    r0.v.cdd = v->v.cdd / r0.v.d;
	    chk_rd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_div_qq_d(v->v.cqq, r0.v.d);
	    chk_rd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_div_cc_d(thr_c, v->v.mpc, r0.v.d);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_div_float(v->v.o, r0.v.d, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_d_trunc2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if ((r0.v.i = ez_trunc_d(thr_zr, 0.0 / r0.v.d)) != MININT)
		r0.t = t_int;
	    else
		set_rz();
	    break;
	case t_int:
	    if ((r0.v.i = ez_trunc_d(thr_zr, v->v.i / r0.v.d)) != MININT)
		r0.t = t_int;
	    else
		set_rz();
	    break;
	case t_float:
	    if ((r0.v.i = ez_trunc_d(thr_zr, v->v.d / r0.v.d)) != MININT)
		r0.t = t_int;
	    else
		set_rz();
	    break;
	case t_mpz:		case t_shz:
	    if ((r0.v.i = ez_trunc_d(thr_zr,
				     mpz_get_d(v->v.mpz) / r0.v.d)) != MININT)
		r0.t = t_int;
	    else
		set_rz();
	    break;
	case t_mpq:		case t_shq:
	    if ((r0.v.i = ez_trunc_d(thr_zr,
				     mpq_get_d(v->v.mpq) / r0.v.d)) != MININT)
		r0.t = t_int;
	    else
		set_rz();
	    break;
	case t_shr:	case t_mpr:
	    if ((r0.v.i = ez_trunc_r_d(thr_zr, v->v.mpr, r0.v.d)) != MININT)
		r0.t = t_int;
	    else
		set_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_trunc_float(v->v.o, r0.v.d, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_d_rem(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.d = fmod(0.0, r0.v.d);
	    break;
	case t_int:
	    r0.v.d = fmod(v->v.i, r0.v.d);
	    break;
	case t_float:
	    r0.v.d = fmod(v->v.d, r0.v.d);
	    break;
	case t_mpz:		case t_shz:
	    r0.v.d = fmod(mpz_get_d(v->v.mpz), r0.v.d);
	    break;
	case t_mpq:		case t_shq:
	    r0.v.d = fmod(mpq_get_d(v->v.mpq), r0.v.d);
	    break;
	case t_mpr:		case t_shr:
	    er_rem_r_d(thr_rr, v->v.mpr, r0.v.d);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_rem_float(v->v.o, r0.v.d, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_d_atan2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.d = atan2(0.0, r0.v.d);
	    break;
	case t_int:
	    r0.v.d = atan2(v->v.i, r0.v.d);
	    break;
	case t_float:
	    r0.v.d = atan2(v->v.d, r0.v.d);
	    break;
	case t_mpz:		case t_shz:
	    r0.v.d = atan2(mpz_get_d(v->v.mpz), r0.v.d);
	    break;
	case t_mpq:		case t_shq:
	    r0.v.d = atan2(mpq_get_d(v->v.mpq), r0.v.d);
	    break;
	case t_mpr:		case t_shr:
	    er_atan2_r_d(thr_rr, v->v.mpr, r0.v.d);
	    set_rr();
	    break;
	case t_cdd:
	    r0.v.cdd = edd_atan2_dd_d(v->v.cdd, r0.v.d);
	    chk_rd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_atan2_qq_d(v->v.cqq, r0.v.d);
	    chk_rd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_atan2_cc_d(thr_c, v->v.mpc, r0.v.d);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_atan2_float(v->v.o, r0.v.d, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_d_pow(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.d = pow(0.0, r0.v.d);
	    break;
	case t_int:
	    if (v->v.i < 0 && finite(r0.v.d) && rint(r0.v.d) != r0.v.d) {
		r0.v.cdd = edd_pow_i_d(v->v.i, r0.v.d);
		chk_rd();
	    }
	    else
		r0.v.d = pow(v->v.i, r0.v.d);
	    break;
	case t_float:
	    if (finite(v->v.d) && v->v.d < 0.0 &&
		finite(r0.v.d) && rint(r0.v.d) != r0.v.d) {
		r0.v.cdd = edd_pow_d_d(v->v.d, r0.v.d);
		chk_rd();
	    }
	    else
		r0.v.d = pow(v->v.d, r0.v.d);
	    break;
	case t_mpz:		case t_shz:
	    if (mpz_sgn(v->v.mpz) < 0 &&
		finite(r0.v.d) && rint(r0.v.d) != r0.v.d) {
		r0.v.cdd = edd_pow_z_d(v->v.mpz, r0.v.d);
		chk_rd();
	    }
	    else
		r0.v.d = ed_pow_z_d(v->v.mpz, r0.v.d);
	    break;
	case t_mpq:		case t_shq:
	    if (mpq_sgn(v->v.mpq) < 0 &&
		finite(r0.v.d) && rint(r0.v.d) != r0.v.d) {
		r0.v.cdd = edd_pow_q_d(v->v.mpq, r0.v.d);
		chk_rd();
	    }
	    else
		r0.v.d = ed_pow_q_d(v->v.mpq, r0.v.d);
	    break;
	case t_mpr:		case t_shr:
	    if (mpfr_number_p(v->v.mpr) &&
		mpfr_number_p(v->v.mpr) && mpfr_sgn(v->v.mpr) < 0 &&
		finite(r0.v.d) && rint(r0.v.d) != r0.v.d) {
		ecc_pow_r_d(thr_c, v->v.mpr, r0.v.d);
		set_chk_rrr();
	    }
	    else {
		er_pow_r_d(thr_rr, v->v.mpr, r0.v.d);
		set_rr();
	    }
	    break;
	case t_cdd:
	    r0.v.cdd = edd_pow_dd_d(v->v.cdd, r0.v.d);
	    chk_rd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_pow_qq_d(v->v.cqq, r0.v.d);
	    chk_rd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_pow_cc_d(thr_c, v->v.mpc, r0.v.d);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_pow_float(v->v.o, r0.v.d, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_d_hypot(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.d = ed_abs_d(r0.v.d);
	    break;
	case t_int:
	    r0.v.d = ed_hypot_i_d(v->v.i, r0.v.d);
	    break;
	case t_float:
	    r0.v.d = ed_hypot_d_d(v->v.d, r0.v.d);
	    break;
	case t_mpz:		case t_shz:
	    r0.v.d = ed_hypot_z_d(v->v.mpz, r0.v.d);
	    break;
	case t_mpq:		case t_shq:
	    r0.v.d = ed_hypot_q_d(v->v.mpq, r0.v.d);
	    break;
	case t_mpr:		case t_shr:
	    er_hypot_r_d(thr_rr, v->v.mpr, r0.v.d);
	    set_rr();
	    break;
	case t_cdd:
	    /* sqrt(a*(conj a) + b*b) */
	    r0.v.d = ed_hypot_dd_d(v->v.cdd, r0.v.d);
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.d = ed_hypot_qq_d(v->v.cqq, r0.v.d);
	    break;
	case t_mpc:		case t_shc:
	    er_hypot_cc_d(thr_rr, v->v.mpc, r0.v.d);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_hypot_float(v->v.o, r0.v.d, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_d_complex(evalue_t *v)
{
    if (likely(r0.v.d)) {
	switch (v->t) {
	    case t_void:
		imag(r0.v.cdd) = r0.v.d;
		real(r0.v.cdd) = 0.0;
		chk_rd();
		break;
	    case t_int:
		imag(r0.v.cdd) = r0.v.d;
		real(r0.v.cdd) = v->v.i;
		chk_rd();
		break;
	    case t_float:
		imag(r0.v.cdd) = r0.v.d;
		real(r0.v.cdd) = v->v.d;
		chk_rd();
		break;
	    case t_mpz:		case t_shz:
		imag(r0.v.cdd) = r0.v.d;
		real(r0.v.cdd) = mpz_get_d(v->v.mpz);
		chk_rd();
		break;
	    case t_mpq:		case t_shq:
		imag(r0.v.cdd) = r0.v.d;
		real(r0.v.cdd) = mpq_get_d(v->v.mpq);
		chk_rd();
		break;
	    case t_mpr:		case t_shr:
		mpfr_set_d(thr_ri, r0.v.d, thr_rnd);
		mpfr_set(thr_rr, v->v.mpr, thr_rnd);
		set_chk_rrr();
		break;
	    default:
		evm_raise(except_not_a_real_number);
	}
    }
    else
	evm_v_complex(v);
}
